package com.logistics.modules.vehicleManagement.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.logistics.common.utils.PageUtils;
import com.logistics.common.utils.R;
import com.logistics.modules.vehicleManagement.entity.VehicleEntity;
import com.logistics.modules.vehicleManagement.service.VehicleService;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.*;


/**
 * 
 *
 * @author HURRICANE
 * @email HURRICANE@nb.com
 * @date 2022-05-07 09:25:16
 */
@RestController
@RequestMapping("vehicleManagement/vehicle")
public class VehicleController {
    @Autowired
    private VehicleService vehicleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("vehicleManagement:vehicle:list")
    public R list(@RequestParam Map<String, Object> params){

        PageUtils page = vehicleService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取在出车中的车辆
     */
    @RequestMapping("/listByStatusFalse")
    @RequiresPermissions("vehicleManagement:vehicle:list")
    public R listByStatusFalse(@RequestParam Map<String, Object> params){

        PageUtils page = vehicleService.listByStatusFalse(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取在空闲中的车辆
     */
    @RequestMapping("/listByStatus")
    @RequiresPermissions("vehicleManagement:vehicle:list")
    public R listByStatus(@RequestParam Map<String, Object> params){

        PageUtils page = vehicleService.queryPageByStatus(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/checkIfTheVehicleIsFree")
    @RequiresPermissions("vehicleManagement:vehicle:list")
    public R checkIfTheVehicleIsFree(){

        Boolean res = vehicleService.checkIfTheVehicleIsFree();

        return R.ok().put("res", res);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("vehicleManagement:vehicle:info")
    public R info(@PathVariable("id") Integer id){

		VehicleEntity vehicle = vehicleService.getById(id);

        return R.ok().put("vehicle", vehicle);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("vehicleManagement:vehicle:save")
    public R save(@RequestBody VehicleEntity vehicle){

		vehicleService.save(vehicle);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("vehicleManagement:vehicle:update")
    public R update(@RequestBody VehicleEntity vehicle){

		vehicleService.updateById(vehicle);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("vehicleManagement:vehicle:delete")
    public R delete(@RequestBody Long[] ids){
        List<Integer> list = new ArrayList<>();
        for (VehicleEntity vehicleEntity: vehicleService.queryList(ids)) {
            Integer vehicleStatus = vehicleEntity.getVehicleStatus();
            list.add(vehicleStatus);
        }

        if(list.contains(0)){
            return R.error("有车辆正在出车中不能删除");
        }

        vehicleService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 获取车辆的饼状图数据
     */
    @RequestMapping("/getPieData")
    @RequiresPermissions("vehicleManagement:vehicle:list")
    public R getPieData(){
        // 获取车辆的饼状图数据
        return vehicleService.getPieData();
    }

}
